package com.emep.emeptime.service;


import com.emep.emeptime.entity.AnnualTotalEmissions;
import com.emep.emeptime.entity.AreaAqiMoniter;
import com.emep.emeptime.entity.DataAqiInfo;
import com.emep.emeptime.entity.api.AqiNumberOfDays;
import com.emep.emeptime.repository.DataAqiInfoRepository;

import com.emep.emeptime.repository.TotalEmissionsRepository;
import com.emep.emeptime.utils.DateTimeUtil;
import com.emep.emeptime.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author: LFY
 * @version:V1.0
 * @Description:
 * @Date: 2018/8/20 9:42
 */

@Service
@Slf4j
public class TotalEmissionsService {

    @Autowired
    private DataAqiInfoRepository dataAqiInfoRepository;
    @Autowired
    private TotalEmissionsRepository totalEmissionsRepository;


    public void save() throws ParseException{
        List<AnnualTotalEmissions> annualTotalEmissionsList= totalEmissionsRepository.findAll();
        AnnualTotalEmissions annualTotalEmissions=new AnnualTotalEmissions();
        List<DataAqiInfo> dataAqiInfoList=new ArrayList<DataAqiInfo>();
        if(annualTotalEmissionsList.size() != 0){
            int newDateYear = 0;
            for(int i = 0;i<annualTotalEmissionsList.size();i++){
                annualTotalEmissions = annualTotalEmissionsList.get(i);
                //当前年份计算
                if(annualTotalEmissions.getYear().equals(DateTimeUtil.getNowDateTimeForString("yyyy"))){
                    newDateYear++;
                    storage(annualTotalEmissions);
                }
            }
            //新的一年，重新创建一条
            if(newDateYear == 0){

                storage(annualTotalEmissions);
            }
        }else {
            //项目第一次运行

            storage(annualTotalEmissions);
        }

    }

  /*  *//**
     * 获取全年数据
     * @return
     *//*
    public List<DataAqiInfo> getAll() {
        return dataAqiInfoRepository.findAll();
    }
*/
    /**
     * 添加
     * @param
     * @throws ParseException
     */
    public void storage(AnnualTotalEmissions annualTotalEmissions)  throws ParseException{
        /**
         * 去重获取城市areaId
         */
        List<DataAqiInfo> dataAqiInfoList = dataAqiInfoRepository.findAll();
        DataAqiInfo dataAqiInfo = new DataAqiInfo();
        List<DataAqiInfo> newList = dataAqiInfoList.stream().distinct().collect(Collectors.toList());
        for(int i = 0;i < newList.size(); i++){
            dataAqiInfo =newList.get(i);

        }
        String areaid=dataAqiInfo.getAreaId();
        double co= 0.00;
        double no2= 0.00;
        double pm10= 0.00;
        double pm25= 0.00;
        double o3= 0.00;
        double so2=0.00;


       /* System.out.println(DateTimeUtil.getStartYear("2018"));
        System.out.println(DateTimeUtil.getEndYear("2018"));*/

        String startdate=DateTimeUtil.getStartYear("2018");
        String enddate=DateTimeUtil.getEndYear("2018");

       /**
        * 获取污染物排放总和
        */
        System.out.println(areaid);
        co= dataAqiInfoRepository.getCoSum(startdate,enddate,areaid);
        no2= dataAqiInfoRepository.getNo2Sum(startdate,enddate,areaid);
       pm10= dataAqiInfoRepository.getPm10Sum(startdate,enddate,areaid);
        pm25= dataAqiInfoRepository.getPm25Sum(startdate,enddate,areaid);
        o3= dataAqiInfoRepository.getO3Sum(startdate,enddate,areaid);
        so2= dataAqiInfoRepository.getSo2Sum(startdate,enddate,areaid);
        annualTotalEmissions.setYear(DateTimeUtil.getYear());
        annualTotalEmissions.setAreaId(areaid);
        annualTotalEmissions.setCo(String.valueOf(co));
        annualTotalEmissions.setNo2(String.valueOf(no2));
        annualTotalEmissions.setPm10(String.valueOf(pm10));
        annualTotalEmissions.setPm25(String.valueOf(pm25));
        annualTotalEmissions.setO3(String.valueOf(o3));
        annualTotalEmissions.setSo2(String.valueOf(so2));
        AnnualTotalEmissions annualTotalEmissions2 = totalEmissionsRepository.save(annualTotalEmissions);
        if (annualTotalEmissions2 != null && !StringUtils.isEmpty(annualTotalEmissions2.getId())) {
            log.info("************ 计算更新成功*********");
        }
    }

}

